package com.jic.product.controller.manage;

import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.common.redis.RedisClientTemplate;
import com.jic.product.after.response.GoodsQueryInfo;
import com.jic.product.after.response.GoodsSpuPageQueryResponse;
import com.jic.product.common.util.BeanUtil;
import com.jic.product.entity.*;
import com.jic.product.manage.request.*;
import com.jic.product.manage.response.*;
import com.jic.product.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品控制器
 *
 * @author wj
 * @date: 2020年01月11日
 */
@RestController
@RequestMapping("/productManagement")
@Api(value = "商品controller", tags = {"商品接口"})
@Slf4j
public class ProductManagementController {

    @Autowired
    ProductService productService;

    @Autowired
    ProductDescribeService productDescribeService;

    @Autowired
    ProductAfterSaleService productAfterSaleService;

    @Autowired
    ProductPriceService productPriceService;

    @Autowired
    ProductDistributeService productDistributeService;

    @Autowired
    ProductGuaranteeRelationService productGuaranteeRelationService;

    @Autowired
    ProductPropertyNameRelationService productPropertyNameRelationService;

    @Autowired
    ProductPropertyValueRelationService productPropertyValueRelationService;

    @Autowired
    ProductSaleAreaRelationService productSaleAreaRelationService;

    @Autowired
    RedisClientTemplate redisClientTemplate;

    @Autowired
    ProductBomService productBomService;

    @ApiOperation(value = "获取所有商品信息")
    @PostMapping("/queryPage")
    @ResponseBody
    public RestResult<PageResult<ProductResponse>> queryPage(@RequestBody ProductRequest form) {
        Page page = new Page();
        page.setLimit(form.getLimit());
        page.setStart(form.getStart());
        Product product = BeanUtil.copy(form, Product.class);

        PageResult<ProductResponse> result = productService.queryPage(product, page);
        PageResult<ProductResponse> pageResult = new PageResult<>();
        pageResult.setRows(result.getRows());
        pageResult.setTotal(result.getTotal());
        return RestResult.success(pageResult);
    }

    @ApiOperation(value = "获取单个商品信息")
    @PostMapping("/getShopInfo")
    @ResponseBody
    public RestResult<ShopInfoResponse> getShopInfo(@RequestBody ShopInfoRequest form) {
        ShopInfoResponse response = productService.getShopInfo(form);
        return RestResult.success(response);
    }

    @ApiOperation(value = "店铺商品列表")
    @PostMapping("/queryShopPage")
    @ResponseBody
    public RestResult<PageResult<ShopInfoResponse>> queryShopPage(@RequestBody ShopInfoRequest form) {

        Page page = new Page();
        page.setLimit(form.getLimit());
        page.setStart(form.getStart());

        PageResult<ShopInfoResponse> result = productService.queryShopPage(form, page);

        PageResult<ShopInfoResponse> pageResult = new PageResult<>();
        pageResult.setRows(result.getRows());
        pageResult.setTotal(result.getTotal());
        return RestResult.success(pageResult);
    }

    @ApiOperation(value = "店铺商品上下架状态修改")
    @PostMapping("/updateShopCanSale")
    public RestResult<Long> update(@RequestBody ShopInfoRequest form) {
        try {
            //请求实体转数据库实体
            Product product = BeanUtil.copy(form, Product.class);
            productService.updateShopCansale(product);
        } catch (Exception e) {
            log.error(e.getLocalizedMessage());
            return RestResult.error("9999", e.getLocalizedMessage());
        }
        return RestResult.success(null);
    }

    @ApiOperation(value = "添加商品基本信息")
    @PostMapping(value = "/addProductInfo")
    @ResponseBody
    public RestResult<Product> addProductInfo(@RequestBody ProductInfoRequest productInfoRequest) {
        //请求实体转数据库实体
        Product product = BeanUtil.copy(productInfoRequest, Product.class);
        product.setDeptId(productInfoRequest.getAdminOperate().getDeptId());
        return productService.addProductInfo(product, productInfoRequest.getGuaranteeId());
    }
    @ApiOperation(value = "添加商品基本信息and价格管理")
    @PostMapping(value = "/addProductPriceInfo")
    @ResponseBody
    public RestResult<Boolean> addProductPriceInfo(@RequestBody ProductInfoPriceRequest request) {
        //请求实体转数据库实体
        Product product = BeanUtil.copy(request, Product.class);
        product.setDeptId(request.getAdminOperate().getDeptId());
        return productService.addProductPriceInfo(product, request.getGuaranteeId());
    }

    @ApiOperation(value = "添加商品文描")
    @PostMapping(value = "/addOrUpdateProductDescribe")
    @ResponseBody
    public RestResult<ProductDescribeResponse> addOrUpdateProductDescribe(
            @RequestBody ProductDescribeRequest productDescribeRequest) {
        ProductDescribeResponse productDescribeResponse = new ProductDescribeResponse();
        try {
            ProductDescribe productDescribe = BeanUtil.copy(productDescribeRequest, ProductDescribe.class);

            if (productDescribe.getId() > 0) {
                productDescribeService.update(productDescribe);
            } else {
                productDescribeService.insert(productDescribe);
            }
            productDescribeResponse = BeanUtil.copy(productDescribe, ProductDescribeResponse.class);
        } catch (Exception e) {
            log.error(e.getLocalizedMessage());
            return RestResult.error("9999", e.getLocalizedMessage());
        }
        return RestResult.success(productDescribeResponse);
    }

    @ApiOperation(value = "添加商品售后")
    @PostMapping(value = "/addOrUpdateProductAfterSale")
    @ResponseBody
    public RestResult<ProductAfterSaleResponse> addOrUpdateProductAfterSale(
            @RequestBody ProductAfterSaleRequest productAfterSaleRequest) {
        ProductAfterSaleResponse productAfterSaleResponse = new ProductAfterSaleResponse();
        try {
            ProductAfterSale productAfterSale = BeanUtil.copy(productAfterSaleRequest, ProductAfterSale.class);
            if (productAfterSale.getId() > 0) {
                productAfterSaleService.update(productAfterSale);
            } else {
                productAfterSaleService.insert(productAfterSale);
            }
            productAfterSaleResponse = BeanUtil.copy(productAfterSale, ProductAfterSaleResponse.class);
        } catch (Exception e) {
            log.error(e.getLocalizedMessage());
            return RestResult.error("9999", e.getLocalizedMessage());
        }
        return RestResult.success(productAfterSaleResponse);
    }

    @ApiOperation(value = "添加商品价格")
    @PostMapping(value = "/addOrUpdateProductPrice")
    @ResponseBody
    public RestResult<ProductPriceResponse> addOrUpdateProductPrice(
            @RequestBody ProductPriceRequest productPriceRequest) {
        ProductPriceResponse productPriceResponse = new ProductPriceResponse();
        try {
            ProductPrice productPrice = BeanUtil.copy(productPriceRequest, ProductPrice.class);
            if (productPrice.getId() > 0) {
                productPriceService.updateProductPrice(productPriceRequest);
            } else {
                productPriceService.insert(productPrice);
            }
            productPriceResponse = BeanUtil.copy(productPrice, ProductPriceResponse.class);
        } catch (Exception e) {
            log.error(e.getLocalizedMessage());
            return RestResult.error("9999", e.getLocalizedMessage());
        }
        return RestResult.success(productPriceResponse);
    }

    @ApiOperation(value = "商品分发")
    @PostMapping(value = "/addProductDistribute")
    @ResponseBody
    public RestResult addProductDistribute(
            @RequestBody ProductDistributeRequest productDistributeRequest) {
        return productService.saveProductDistribute(productDistributeRequest);
    }

    @ApiOperation(value = "商品分发到店铺")
    @PostMapping(value = "/saveProductDistributeStore")
    @ResponseBody
    public RestResult saveProductDistributeStore(
            @RequestBody ProductDistributeStoreRequest productDistributeStoreRequest) {
        return productService.saveProductDistributeStore(productDistributeStoreRequest);
    }

    @ApiOperation(value = "系列子品列表查询")
    @PostMapping(value = "/getProductParent")
    @ResponseBody
    public RestResult<List<ProductParentResponse>> getProductParent(
            @RequestBody GetProductParentRequest getProductParentRequest) {
        return productService.getProductParent(getProductParentRequest);
    }

    @ApiOperation(value = "保存系列子品")
    @PostMapping(value = "/saveProductParent")
    @ResponseBody
    public RestResult<Boolean> saveProductParent(@RequestBody ProductParentRequest productParentRequest) {
        return productService.saveProductParent(productParentRequest );
    }

    @ApiOperation(value = "更新系列子品价格")
    @PostMapping(value = "/updateProductParentPrice")
    @ResponseBody
    public RestResult<Boolean> updateProductParentPrice(
            @RequestBody ProductParentPriceRequest productParentPriceRequest) {
        return productService.updateProductParentPrice(productParentPriceRequest);
    }

    @ApiOperation(value = "删除系列子品")
    @GetMapping(value = "/deleteProductParent")
    @ResponseBody
    public RestResult deleteProductParent(Long productId) {
        return productService.deleteProductParent(productId);
    }

    @ApiOperation(value = "商品商家渠道修改")
    @PostMapping(value = "/updateProductDistributeStore")
    @ResponseBody
    public RestResult<Boolean> updateProductDistributeStore(
            @RequestBody ProductDistributeStoreRequest productDistributeStoreRequest) {
        return productService.updateProductDistributeStore(productDistributeStoreRequest);
    }

    @ApiOperation(value = "获取所有商品信息")
    @GetMapping("/queryProductInfo")
    @ResponseBody
    public RestResult<ProductResponse> queryProductInfo(Long productId) {
        ProductResponse productResponse = productService.queryProductInfo(productId);
        //根据商品ID 查询商品保障
        ProductGuaranteeRelation productGuaranteeRelation = new ProductGuaranteeRelation();
        productGuaranteeRelation.setProductId(productResponse.getId());
        productResponse
                .setGuaranteeId(productGuaranteeRelationService.guaranteeId(productGuaranteeRelation));
        return RestResult.success(productResponse);
    }

    @ApiOperation(value = "更新商品关联")
    @PostMapping("/updateProductInfo")
    @ResponseBody
    public RestResult updateProductInfo(@RequestBody ProductInfoUpdateRequest productInfoUpdateRequest) {
        productService.updateProductInfo(productInfoUpdateRequest);
        return RestResult.success(true);
    }

    @ApiOperation(value = "进目录获取所有商品信息")
    @PostMapping("/queryProductCatalog")
    public RestResult<List<ProductResponse>> queryProductCatalog(@RequestBody ProductRequest form) {
        Product product = BeanUtil.copy(form, Product.class);
        List<ProductResponse> result = productService.queryProductCatalog(product);
        return RestResult.success(result);
    }

    @ApiOperation(value = "店铺商品列表")
    @PostMapping("/queryShopProductPage")
    @ResponseBody
    public RestResult<PageResult<ShopProductInfoResponse>> queryShopProductPage(@RequestBody ShopInfoRequest form) {

        Page page = new Page();
        page.setLimit(form.getLimit());
        page.setStart(form.getStart());

        PageResult<ShopProductInfoResponse> result = productService.queryShopProductPage(form, page);

        PageResult<ShopProductInfoResponse> pageResult = new PageResult<>();
        pageResult.setRows(result.getRows());
        pageResult.setTotal(result.getTotal());
        return RestResult.success(pageResult);
    }

    @ApiOperation("查询商品列表-分页(营销中心)")
    @PostMapping("/query-product-list")
    public RestResult<PageResult<GoodsSpuPageQueryResponse>> queryProductList(@RequestBody GoodsSpuPageQueryRequest request) {
        return productService.queryProductList(request);
    }

    @ApiOperation("商品详情-bom商品")
    @PostMapping("/queryBomProduct")
    public RestResult<List<ProductBomResponse>> queryBomProduct(@RequestBody ProductBomRequest productBomRequest) {
        return productBomService.query(productBomRequest);
    }

    @ApiOperation("商品详情-新增bom商品")
    @PostMapping("/addBomProduct")
    public RestResult<String> addBomProduct(@RequestBody ProductBomBatchRequest productBomBatchRequest) {
        return productBomService.insert(productBomBatchRequest);
    }

    @ApiOperation("商品详情-修改bom商品")
    @PostMapping("/deleteBomProduct")
    public RestResult<String> deleteBomProduct(@RequestBody ProductBomRequest productBomRequest) {
        ProductBom productBom = new ProductBom();
        BeanUtils.copyProperties(productBomRequest, productBom);
        return productBomService.delete(productBom);
    }

    @ApiOperation("商品详情-修改bom商品")
    @PostMapping("/updateBomProduct")
    public RestResult<String> updateBomProduct(@RequestBody ProductBomRequest productBomRequest) {
        ProductBom productBom = new ProductBom();
        BeanUtils.copyProperties(productBomRequest, productBom);
        return productBomService.update(productBom);
    }

    @ApiOperation("查询商品sku列表-分页(营销中心)")
    @PostMapping("/querySkuPageByPromotionId")
    public RestResult<PageResult<GoodsQueryInfo>> querySkuPageByPromotionId(
            @RequestBody GoodsSpuPageQueryRequest request) {
        return productService.querySkuPageByPromotionId(request);
    }

    @ApiOperation("平台批量分发商品给商家")
    @PostMapping("/batchPlatformProduct")
    public RestResult<String> batchPlatformProduct(@RequestBody BatchProductRequest batchProductRequest) {
        return productService.batchPlatformProduct(batchProductRequest);
    }

    @ApiOperation("商家批量分发商品给店铺")
    @PostMapping("/batchMerchantProduct")
    public RestResult<String> batchMerchantProduct(@RequestBody BatchProductRequest batchProductRequest) {
        return productService.batchMerchantProduct(batchProductRequest);
    }

    @ApiOperation("根据类目Id查询当前类目所有商品")
    @PostMapping("/queryProductByCategoryId")
    public RestResult<PageResult<ProductResponse>> queryProductByCategoryId(@RequestBody ProductRequest productRequest) {
        return productService.queryProductByCategoryId(productRequest);
    }


}

